package com.lion.common.spring.openapi;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

/**
 * @author Chris Chan
 * Create On 2025/7/20 下午11:51
 * Use for: 构建默认的OpenAPI文档
 * Explain:
 */
@Configuration
public class OpenApiDefaultConfiguration {

    /**
     * 构建默认的OpenAPI文档
     *
     * @return 默认的OpenAPI文档
     */
    @Bean
    public OpenAPI buildDefaultOpenApi() {
        // 配置安全方案
        Components securitySchemesComponents = new Components()
                .addSecuritySchemes(
                        HttpHeaders.AUTHORIZATION,
                        new SecurityScheme()
                                .name(HttpHeaders.AUTHORIZATION)
                                .type(SecurityScheme.Type.APIKEY)
                                .in(SecurityScheme.In.HEADER)
                                .scheme("Bearer")
                                .bearerFormat("JWT")
                );
        return new OpenAPI()
                .info(new Info()
                        .title("红宝石茶杯后端服务接口文档")
                        .description("红宝石茶杯后端服务接口文档")
                        .version("1.0.0")
                        .termsOfService("http://localhost:8080")
                        .contact(new Contact()
                                .name("Chris Chan")
                                .url("http://localhost:8080")
                                .email("chris.chan@lion.com"))
                        .license(new License()
                                .name("Apache 2.0")
                                .url("https://www.apache.org/licenses/LICENSE-2.0.html"))
                )
                // 配置安全方案
                .components(securitySchemesComponents)
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
    }

}
